<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class User extends CMiniyunModel
{
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	
	public function tableName()
	{
		return Yii::app()->params['tablePrefix'].'users';
	}


	public function relations()
	{
						return array(
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'user_name' => '用户名',
    		'user_pass' => '用户密码',
            'password_confirm' => '确认密码',
            'user_status' => '用户状态',
			'created_at' => '创建时间',
		);
	}
	
	public function search()
	{

		$criteria=new CDbCriteria;

		$criteria->compare('user_uuid',$this->user_uuid,true);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_pass',$this->user_pass,true);
		$criteria->compare('user_status',$this->user_status);
		$criteria->compare('created_at',$this->created_at,true);
		$criteria->compare('updated_at',$this->updated_at,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getByUserAndPasswd($username,$passwd){
		$user = $this->find("user_name=? and user_pass=? and user_status=1",array($username,CSecurity::encryPasswd($passwd)));
		return $user;
	}



	
	public function beforeSave()
	{
		if(parent::beforeSave()){
			if(!isset($this->user_status))
			{
				$this->user_status=1;
			}
			return true;
		}
		return false;
	}

	
	public function adminCount(){
		return $this->count("id in(select user_id from ".Yii::app()->params['tablePrefix']."user_metas where meta_key='is_admin' and meta_value='1')");
	}

	
	public function disabledCount(){
		return $this->count("user_status=0");
	}
	
	public function disabledUsers($userIds){
		$this->updateAll(array("user_status"=>"0"), "id in(".$userIds.")");
	}
	
	public function enabledUsers($userIds){
		$this->updateAll(array("user_status"=>"1"), "id in(".$userIds.")");
	}
	
	public function changeRole($userIds,$isAdmin){
		if($userIds!='' && strlen($userIds)>0){
			$idsArray = explode(",",$userIds);
			foreach($idsArray as $index=>$userId){
				$userMeta = new UserMeta();
				$item = $userMeta->find("meta_key='is_admin' and user_id=".$userId);
				if(isset($item)){
					$userMeta = $item;
				}else{
					$userMeta["meta_key"]="is_admin";
					$userMeta["user_id"]=$userId;
					$userMeta["created_at"]=date("Y-m-d H:i:s");
					$userMeta["updated_at"]=date("Y-m-d H:i:s");
				}
				$userMeta["meta_value"]=$isAdmin?"1":"0";
				$userMeta->save();
			}
		}
	}
	
	public function deleteUsers($userIds){
		if($userIds!='' && strlen($userIds)>0){
						$userFile = new UserFile();
			$userFile->deleteUserFile($userIds);
						$userMeta = new UserMeta();
			$userMeta->deleteUserMeta($userIds);
						$event = new Event();
			$event->deleteEvent($userIds);
						$userDevice = new UserDevice();
			$userDevice->deleteUserDevice($userIds);
						$this->deleteAll("id in(".$userIds.")");

		}
	}
}